; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes='dse' -S %s | FileCheck %s

target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

; Test case from PR50220.
define i32 @other_value_escapes_before_call() {
; CHECK-LABEL: @other_value_escapes_before_call(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[V1:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[V2:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 0, ptr [[V1]], align 4
; CHECK-NEXT:    call void @escape(ptr nonnull [[V1]])
; CHECK-NEXT:    [[CALL:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 [[CALL]], ptr [[V2]], align 4
; CHECK-NEXT:    call void @escape(ptr nonnull [[V2]])
; CHECK-NEXT:    [[LOAD_V2:%.*]] = load i32, ptr [[V2]], align 4
; CHECK-NEXT:    [[LOAD_V1:%.*]] = load i32, ptr [[V1]], align 4
; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[LOAD_V2]], [[LOAD_V1]]
; CHECK-NEXT:    ret i32 [[ADD]]
;
entry:
  %v1 = alloca i32, align 4
  %v2 = alloca i32, align 4
  store i32 0, ptr %v1, align 4
  call void @escape(ptr nonnull %v1)
  store i32 55555, ptr %v2, align 4
  %call = call i32 @getval()
  store i32 %call, ptr %v2, align 4
  call void @escape(ptr nonnull %v2)
  %load.v2 = load i32, ptr %v2, align 4
  %load.v1 = load i32, ptr %v1, align 4
  %add = add nsw i32 %load.v2, %load.v1
  ret i32 %add
}

declare void @escape(ptr)

declare i32 @getval()

declare void @escape_and_clobber(ptr)
declare void @escape_writeonly(ptr) writeonly
declare void @clobber()

define i32 @test_not_captured_before_call_same_bb() {
; CHECK-LABEL: @test_not_captured_before_call_same_bb(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  call void @escape_and_clobber(ptr %a)
  ret i32 %r
}

define i32 @test_not_captured_before_call_same_bb_escape_unreachable_block() {
; CHECK-LABEL: @test_not_captured_before_call_same_bb_escape_unreachable_block(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
; CHECK:       unreach:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 0
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  call void @escape_and_clobber(ptr %a)
  ret i32 %r

unreach:
  call void @escape_and_clobber(ptr %a)
  ret i32 0
}

define i32 @test_captured_and_clobbered_after_load_same_bb_2() {
; CHECK-LABEL: @test_captured_and_clobbered_after_load_same_bb_2(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  call void @escape_and_clobber(ptr %a)
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %r
}

define i32 @test_captured_after_call_same_bb_2_clobbered_later() {
; CHECK-LABEL: @test_captured_after_call_same_bb_2_clobbered_later(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  call void @escape_writeonly(ptr %a)
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %r
}

define i32 @test_captured_sibling_path_to_call_other_blocks_1(i1 %c.1) {
; CHECK-LABEL: @test_captured_sibling_path_to_call_other_blocks_1(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       else:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ 0, [[THEN]] ], [ [[R]], [[ELSE]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %else

then:
  call void @escape_writeonly(ptr %a)
  br label %exit

else:
  %r = call i32 @getval()
  br label %exit

exit:
  %p = phi i32 [ 0, %then ], [ %r, %else ]
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %p
}

define i32 @test_captured_before_call_other_blocks_2(i1 %c.1) {
; CHECK-LABEL: @test_captured_before_call_other_blocks_2(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       else:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ 0, [[THEN]] ], [ [[R]], [[ELSE]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %else

then:
  br label %exit

else:
  call void @escape_and_clobber(ptr %a)
  %r = call i32 @getval()
  br label %exit

exit:
  %p = phi i32 [ 0, %then ], [ %r, %else ]
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %p
}

define i32 @test_captured_before_call_other_blocks_4(i1 %c.1) {
; CHECK-LABEL: @test_captured_before_call_other_blocks_4(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ 0, [[THEN]] ], [ [[R]], [[ENTRY:%.*]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
entry:
  %a = alloca i32, align 4
  store i32 55, ptr %a
  call void @escape_writeonly(ptr %a)
  %r = call i32 @getval()
  br i1 %c.1, label %then, label %exit

then:
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %p = phi i32 [ 0, %then ], [ %r, %entry ]
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %p
}

define i32 @test_captured_before_call_other_blocks_5(i1 %c.1) {
; CHECK-LABEL: @test_captured_before_call_other_blocks_5(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[R]]
;
entry:
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %exit

then:
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %r
}

define i32 @test_captured_before_call_other_blocks_6(i1 %c.1) {
; CHECK-LABEL: @test_captured_before_call_other_blocks_6(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[R]]
;
entry:
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %exit

then:
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  call void @escape_writeonly(ptr %a)
  call void @clobber()
  ret i32 %r
}

define i32 @test_not_captured_before_call_other_blocks_1(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_1(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       else:
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  br i1 %c.1, label %then, label %else

then:
  br label %exit

else:
  br label %exit

exit:
  call void @escape_and_clobber(ptr %a)
  ret i32 %r
}

define i32 @test_not_captured_before_call_other_blocks_2(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_2(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       else:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  br i1 %c.1, label %then, label %else

then:
  call void @escape_and_clobber(ptr %a)
  br label %exit

else:
  call void @escape_and_clobber(ptr %a)
  br label %exit

exit:
  ret i32 %r
}

define i32 @test_not_captured_before_call_other_blocks_3(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_3(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       else:
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  br i1 %c.1, label %then, label %else

then:
  call void @escape_and_clobber(ptr %a)
  br label %exit

else:
  br label %exit

exit:
  ret i32 %r
}

define i32 @test_not_captured_before_call_other_blocks_4(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_4(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       else:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ 0, [[THEN]] ], [ [[R]], [[ELSE]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %else

then:
  br label %exit

else:
  %r = call i32 @getval()
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %p = phi i32 [ 0, %then ], [ %r, %else ]
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %p
}

define i32 @test_not_captured_before_call_other_blocks_5(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_5(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK:       then:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ [[R]], [[THEN]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
entry:
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %exit

then:
  %r = call i32 @getval()
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %p = phi i32 [ %r, %then ], [ 0, %entry ]
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %p
}

define i32 @test_not_captured_before_call_other_blocks_6(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_6(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK:       then:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ [[R]], [[THEN]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
entry:
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br i1 %c.1, label %then, label %exit

then:
  %r = call i32 @getval()
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %p = phi i32 [ %r, %then ], [ 0, %entry ]
  store i32 99, ptr %a, align 4
  call void @escape_writeonly(ptr %a)
  call void @clobber()
  ret i32 %p
}

define i32 @test_not_captured_before_call_other_blocks_7(i1 %c.1) {
; CHECK-LABEL: @test_not_captured_before_call_other_blocks_7(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK:       then:
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[P:%.*]] = phi i32 [ 0, [[THEN]] ], [ [[R]], [[ENTRY:%.*]] ]
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret i32 [[P]]
;
entry:
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  call void @escape_writeonly(ptr %a)
  br i1 %c.1, label %then, label %exit

then:
  call void @escape_writeonly(ptr %a)
  br label %exit

exit:
  %p = phi i32 [ 0, %then ], [ %r, %entry ]
  store i32 99, ptr %a, align 4
  call void @clobber()
  ret i32 %p
}

define i32 @test_not_captured_before_call_same_bb_but_read() {
; CHECK-LABEL: @test_not_captured_before_call_same_bb_but_read(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    [[LV:%.*]] = load i32, ptr [[A]], align 4
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    [[RES:%.*]] = add i32 [[R]], [[LV]]
; CHECK-NEXT:    ret i32 [[RES]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  %r = call i32 @getval()
  %lv = load i32, ptr %a
  store i32 99, ptr %a, align 4
  call void @escape_and_clobber(ptr %a)
  %res = add i32 %r, %lv
  ret i32 %res
}

define i32 @test_captured_after_loop(i1 %c.1) {
; CHECK-LABEL: @test_captured_after_loop(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    br label [[LOOP:%.*]]
; CHECK:       loop:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK:       exit:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br label %loop

loop:
  %r = call i32 @getval()
  store i32 99, ptr %a, align 4
  br i1 %c.1, label %loop, label %exit

exit:
  call void @escape_and_clobber(ptr %a)
  ret i32 %r
}

define i32 @test_captured_in_loop(i1 %c.1) {
; CHECK-LABEL: @test_captured_in_loop(
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    store i32 55, ptr [[A]], align 4
; CHECK-NEXT:    br label [[LOOP:%.*]]
; CHECK:       loop:
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval()
; CHECK-NEXT:    call void @escape_writeonly(ptr [[A]])
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK:       exit:
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = alloca i32, align 4
  store i32 55, ptr %a
  br label %loop

loop:
  %r = call i32 @getval()
  call void @escape_writeonly(ptr %a)
  store i32 99, ptr %a, align 4
  br i1 %c.1, label %loop, label %exit

exit:
  call void @escape_and_clobber(ptr %a)
  ret i32 %r
}

declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
define void @test_escaping_store_removed(ptr %src, ptr %escape) {
; CHECK-LABEL: @test_escaping_store_removed(
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[A:%.*]] = alloca i64, align 8
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr [[A]], ptr [[SRC:%.*]], i64 8, i1 false)
; CHECK-NEXT:    store ptr [[A]], ptr [[ESCAPE:%.*]], align 8
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    store i64 99, ptr [[A]], align 8
; CHECK-NEXT:    call void @clobber()
; CHECK-NEXT:    ret void
;
bb:
  %a = alloca i64, align 8
  store i64 0, ptr %a
  call void @clobber()
  call void @llvm.memcpy.p0.p0.i64(ptr %a, ptr %src, i64 8, i1 false)
  store ptr %a, ptr %escape, align 8
  store ptr %a, ptr %escape, align 8
  call void @clobber()
  store i64 99, ptr %a
  call void @clobber()
  ret void
}


define void @test_invoke_captures() personality ptr undef {
; CHECK-LABEL: @test_invoke_captures(
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT:    invoke void @clobber()
; CHECK-NEXT:    to label [[BB2:%.*]] unwind label [[BB5:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    store i32 0, ptr [[A]], align 8
; CHECK-NEXT:    invoke void @escape(ptr [[A]])
; CHECK-NEXT:    to label [[BB9:%.*]] unwind label [[BB10:%.*]]
; CHECK:       bb4:
; CHECK-NEXT:    ret void
; CHECK:       bb5:
; CHECK-NEXT:    [[LP_1:%.*]] = landingpad { ptr, i32 }
; CHECK-NEXT:    cleanup
; CHECK-NEXT:    ret void
; CHECK:       bb9:
; CHECK-NEXT:    ret void
; CHECK:       bb10:
; CHECK-NEXT:    [[LP_2:%.*]] = landingpad { ptr, i32 }
; CHECK-NEXT:    cleanup
; CHECK-NEXT:    unreachable
;
bb:
  %a = alloca i32
  store i32 99, ptr %a
  invoke void @clobber()
  to label %bb2 unwind label %bb5

bb2:
  store i32 0, ptr %a, align 8
  invoke void @escape(ptr %a)
  to label %bb9 unwind label %bb10

bb4:
  ret void

bb5:
  %lp.1 = landingpad { ptr, i32 }
  cleanup
  ret void

bb9:
  ret void

bb10:
  %lp.2 = landingpad { ptr, i32 }
  cleanup
  unreachable
}

declare noalias ptr @alloc() nounwind
declare i32 @getval_nounwind() nounwind

define i32 @test_not_captured_before_load_same_bb_noalias_call() {
; CHECK-LABEL: @test_not_captured_before_load_same_bb_noalias_call(
; CHECK-NEXT:    [[A:%.*]] = call ptr @alloc()
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval_nounwind()
; CHECK-NEXT:    store i32 99, ptr [[A]], align 4
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
;
  %a = call ptr @alloc()
  store i32 55, ptr %a
  %r = call i32 @getval_nounwind()
  store i32 99, ptr %a, align 4
  call void @escape_and_clobber(ptr %a)
  ret i32 %r
}

define i32 @test_not_captured_before_load_same_bb_noalias_arg(ptr noalias %a) {
; CHECK-LABEL: @test_not_captured_before_load_same_bb_noalias_arg(
; CHECK-NEXT:    [[R:%.*]] = call i32 @getval_nounwind()
; CHECK-NEXT:    store i32 99, ptr [[A:%.*]], align 4
; CHECK-NEXT:    call void @escape_and_clobber(ptr [[A]])
; CHECK-NEXT:    ret i32 [[R]]
;
  store i32 55, ptr %a
  %r = call i32 @getval_nounwind()
  store i32 99, ptr %a, align 4
  call void @escape_and_clobber(ptr %a)
  ret i32 %r
}
